package com.hualan.service.b_qishou;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.Community;
import com.hualan.entity.QiShou;
import com.hualan.entity.Result;
import com.hualan.entity.ResultDate;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@WebServlet(urlPatterns = {"/panghudaojiaBackend/b_qishou/qishouUpdate","/panghudaojiaBackend/b_qishou/qishouList","/panghudaojiaBackend/b_qishou/qishouRemove"})
public class QiShouService extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/b_qishou/qishouUpdate")) {
            int flag = Integer.parseInt(req.getParameter("addOrUpdateFlag"));
            if (flag == 1) {
                //添加骑手
                String sql = "insert into b_qishou values(null,?,?,1,?,0)";
                List<Object> list = new ArrayList<>();
                list.add(req.getParameter("deliverymanName"));
                list.add(req.getParameter("deliverymanPhone"));
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                list.add(LocalDateTime.now().format(formatter));
                ExecUtil.execute(sql, list, req, true);
                ExecUtil.commit();
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200, "增加成功！")));
            }else if(flag == 2){
                //修改骑手
                String sql = "update b_qishou set deliverymanName = ?,deliverymanName=?,deliverymanPhone=?,deliverymanStatus=?,deliveryOrderCount=? where deliverymanId = ?;";
                List<Object> list = new ArrayList<>();
                list.add(req.getParameter("deliverymanName"));
                list.add(req.getParameter("deliverymanName"));
                list.add(req.getParameter("deliverymanPhone"));
                list.add(req.getParameter("deliverymanStatus"));
                list.add(req.getParameter("deliveryOrderCount"));
                list.add(req.getParameter("deliverymanId"));
                ExecUtil.execute(sql,list,req,true);
                ExecUtil.commit();
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200,"修改成功！")));
            }
        }
        //删除骑手
        if (location.equals("/panghudaojiaBackend/b_qishou/qishouRemove")) {
            String sql = "delete from b_qishou where deliverymanId = ?";
            List<Object> list = new ArrayList<>();
            list.add(req.getParameter("deliverymanId"));
            ExecUtil.execute(sql,list,req,true);
            ExecUtil.commit();
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,"删除成功")));
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询骑手数据
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/b_qishou/qishouList")){
            String sql = "select * from b_qishou where ";
            List<Object> list = new ArrayList<>();//装外界传的参数
            if(!Objects.equals(req.getParameter("name"), "")){
                sql += "deliverymanName like ? and ";
                list.add("%"+req.getParameter("name")+"%");
            }
            if(!Objects.equals(req.getParameter("phone"), "")){
                sql += "deliverymanPhone like ? and ";
                list.add("%"+req.getParameter("phone")+"%");
            }
            if(!Objects.equals(req.getParameter("startTime"), "") && !Objects.equals(req.getParameter("endTime"), "")){
                sql += "addTime between ? and ? and ";
                list.add(req.getParameter("startTime"));
                list.add(req.getParameter("endTime"));
            }
            sql+="\'a\'=\'a\'";
            System.out.println(sql);
            Integer pageNum = Integer.parseInt(req.getParameter("pageNum"));
            Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
            Integer start = pageSize*(pageNum-1);
            Integer end = pageSize*pageNum;
            sql += "limit "+start+","+end;
            ResultSet resultSet =(ResultSet) ExecUtil.execute(sql,list,req,false);
            ExecUtil.commit();
            List<Object> qishous = new ArrayList<>();
            try{
                while (resultSet.next()){
                    EntityFactory entityFactory = new EntityFactory(QiShou.class);
                        QiShou qiShou =(QiShou) entityFactory.startCombine(resultSet);
                    qishous.add(qiShou);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ExecUtil.close();
            sql = "select count(deliverymanId) total from b_qishou";
            list.clear();
            ResultSet resultSet1 = (ResultSet) ExecUtil.execute(sql,list,req,false);
            ExecUtil.commit();
            int total = 0;
            try {
                while (resultSet1.next()){
                    total = resultSet1.getInt(1);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,new ResultDate(pageNum,pageSize,total,total/pageSize+(total%pageSize!=0?1:0),qishous))));
        }


    }
}
